//
//  XFBTextView.m
//  xfb
//
//  Created by XFB on 2022/4/2.
//  Copyright © 2022 XFB. All rights reserved.
//

#import "XFBTextView.h"

@interface XFBTextView ()<UITextViewDelegate> {
    // 占位label
    UILabel *_placeholderLabel;
}

@end

@implementation XFBTextView

- (instancetype)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        [self setupSubViews];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setupSubViews];
    }
    return self;
}

- (void)setupSubViews {
    // 去除上左边距
    self.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
    // 内容缩进为0 去除左右边距
    self.textContainer.lineFragmentPadding = 0;
    
    // 添加通知监听，文字改变
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidChangeValue:) name:UITextViewTextDidChangeNotification object:self];

    // 占位标签label
    _placeholderLabel = [[UILabel alloc] init];
    _placeholderLabel.textColor = [UIColor blackColor];
    _placeholderLabel.numberOfLines = 0;
    [self addSubview:_placeholderLabel];
    [self performSelector:@selector(setPlaceholderLabelEvent) withObject:nil afterDelay:0.1];
}

// 监听
- (void)textViewDidChangeValue:(NSNotification *)notification {
    if (!_placeholder.length) {
        _placeholderLabel.hidden = YES;
    } else {
        if (self.text.length) {
            _placeholderLabel.hidden = YES;
        } else {
            _placeholderLabel.hidden = NO;
        }
        _placeholderHidden = _placeholderLabel.hidden;
        [self setNeedsDisplay];
    }
}

// 设置占位文字标签
- (void)setPlaceholderLabelEvent {
    // 占位文字字体
    _placeholderLabel.font = _placeholderFont ? _placeholderFont : (self.font ? self.font : [UIFont systemFontOfSize:13.f]);

    UIEdgeInsets insets = self.textContainerInset;
    // 重新调整位置
    CGRect labelFrame = _placeholderLabel.frame;
    labelFrame.origin.x = insets.left;
    _placeholderLabel.frame = labelFrame;
    
    // 占位文字 显示隐藏 条件判断
    if (self.text.length) _placeholderLabel.hidden = YES;
    // 设置占位文字
    if (_placeholderLabel.text.length > 0) [self setPlaceholder:_placeholderLabel.text];
}

// MARK: - Setter
- (void)setPlaceholder:(NSString *)placeholder {
    _placeholder = placeholder;
    
    // 占位文字显示赋值 和 隐藏
    if ([placeholder isEqualToString:@""] || placeholder.length == 0 || placeholder == nil) {
        _placeholderLabel.hidden = YES;
    } else {
        _placeholderLabel.hidden = NO;
        _placeholderLabel.text = placeholder;
    }

    // 内边距
    UIEdgeInsets insets = self.textContainerInset;
    NSDictionary *attributes = @{ NSFontAttributeName:_placeholderLabel.font };
    NSInteger options = NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin;
    CGRect rect = [placeholder boundingRectWithSize:CGSizeMake(self.frame.size.width, MAXFLOAT) options:options attributes:attributes context:NULL];
    CGRect frame = CGRectMake(insets.left, insets.top, rect.size.width, rect.size.height);
    _placeholderLabel.frame = frame;
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor {
    _placeholderColor = placeholderColor;
    _placeholderLabel.textColor = placeholderColor;
}

- (void)setPlaceholderFont:(UIFont *)placeholderFont {
    _placeholderFont = placeholderFont;
    _placeholderLabel.font = placeholderFont;
}

- (void)setPlaceholderHidden:(BOOL)placeholderHidden {
    _placeholderHidden = placeholderHidden;
    _placeholderLabel.hidden = placeholderHidden;
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
}

- (void)setTextFont:(UIFont *)textFont {
    _textFont = textFont;
    self.font = textFont;
}

- (void)placeholderCheckText {
    NSString *text = [self.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (text.length) {
        _placeholderLabel.hidden = YES;
    } else {
        _placeholderLabel.hidden = NO;
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [_placeholderLabel removeFromSuperview];
}

@end
